<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
            <el-form-item label="楼号" prop="num">
                <el-input v-model="queryParams.num" placeholder="请输入楼号" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="社区id" prop="dept_id">
                <el-input v-model="queryParams.dept_id" placeholder="请输入社区id" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['building:building:add']">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="buildingList">
            <el-table-column label="楼号" align="center" prop="num" />
            <el-table-column label="社区id" align="center" prop="dept_id" />
            <el-table-column label="创建者" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="create_time">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="更新者" align="center" prop="updateBy" />
            <el-table-column label="更新时间" align="center" prop="update_time">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.updateTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['building:building:edit']">修改 </el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['building:building:remove']">删除 </el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <el-dialog title="提示" :visible.sync="dialogVisible" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="楼号" prop="num">
                    <el-input v-model="form.num" placeholder="请输入楼号" />
                </el-form-item>
                <el-form-item label="社区id" prop="dept_id">
                    <el-input v-model="form.dept_id" placeholder="请输入社区id" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    import { selectBuildingList, addBuilding, updateBuilding, deleteById } from "@/api/building/building";
    export default {
        name: "building",
        dicts: ["sys_user_sex"],
        data() {
            return {
                showSearch: true,
                loading: true,
                total: 0,
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    num: undefined,
                    dept_id: undefined
                },
                ids: [],
                dialogVisible: false,
                form: {
                    id: undefined,
                    num: undefined,
                    dept_id: undefined,
                    create_by: undefined,
                    create_time: undefined,
                    update_by: undefined,
                    update_time: undefined,
                    remark: undefined
                },
                buildingList: [],
                rules: {
                    num: [
                        { required: true, message: "楼号不能为空", trigger: "blur" }
                    ],
                    dept_id: [
                        { required: true, message: "社区id不能为空", trigger: "blur" }
                    ]
                },
            }

        },
        created() {
            this.getList();
        },
        methods: {
            // 模糊查询
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.queryParams = {
                    pageNum: 1,
                    pageSize: 10,
                    num: undefined,
                    dept_id: undefined
                };
                this.resetForm("queryForm");
                this.handleQuery();
            },
            getList() {
                selectBuildingList(this.queryParams).then(response => {
                    this.buildingList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                }

                );
            },
            // 取消按钮
            cancel() {
                this.dialogVisible = false;
                this.reset();
            },
            /** 新增按钮操作 */
            handleAdd(row) {
                this.reset();
                this.dialogVisible = true;
            },
            // 修改
            handleUpdate(row) {
                this.reset();
                this.dialogVisible = true;
                this.form = row;
            },
            // 删除
            handleDelete(row) {
                const id = row.id;
                this.$modal.confirm('是否确认删除学生编号为"' + id + '"的数据项？').then(function () {
                    return deleteById(id);
                }).then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => { });
            },
            reset() {
                this.form = {
                    id: undefined,
                    num: undefined,
                    dept_id: undefined,
                    create_by: undefined,
                    create_time: undefined,
                    update_by: undefined,
                    update_time: undefined,
                    remark: undefined
                };
                this.resetForm("form");
            },
            /** 提交按钮 */
            submitForm: function () {
                this.$refs["form"].validate(valid => {
                    if(valid){
                        if (this.form.id !== undefined) {
                        updateBuilding(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.dialogVisible = false;
                            this.getList();
                        });
                    } else {
                        addBuilding(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.dialogVisible = false;
                            this.getList();
                        });
                    }
                    }
                });

            },
        }
    }
</script>

<style scoped>

</style>